package org.dromara.comm.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.comm.domain.AsReportImage;
import org.dromara.comm.domain.AsReportManagement;
import org.dromara.comm.enums.Community;
import org.dromara.comm.enums.State;
import org.dromara.comm.enums.Vaccines;
import org.dromara.comm.mapper.AsReportImageMapper;
import org.dromara.comm.mapper.AsReportManagementMapper;
import org.dromara.comm.service.AsReportManagementService;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 报事管理业务层
 *
 * @author: 郭伟明
 * @date: 2024-07-31 19:20
 **/
@Service
public class AsReportManagementServiceImpl extends ServiceImpl<AsReportManagementMapper, AsReportManagement> implements AsReportManagementService {

    @Autowired
    private AsReportManagementMapper asReportManagementMapper;

    @Autowired
    private AsReportImageMapper asReportImageMapper;

    /**
     * 查询枚举类
     * @return
     */
    public List<Community> communityList() {
        return Community.getAllCommunities();
    }

    public List<Vaccines> vaccinesList() {
        return Vaccines.getAllVaccines();
    }

    @Override
    public void insertReportManagement(AsReportManagement asReportManagement, List<String> imagePaths) {
        // 创建并保存报告管理记录
        asReportManagement.setUserId(LoginHelper.getUserId());
        asReportManagementMapper.insert(asReportManagement);
        // 遍历图片路径列表并保存每张图片记录
        for (String imagePath : imagePaths) {
            AsReportImage image = new AsReportImage();
            image.setReportId(asReportManagement.getId());
            image.setImage(imagePath); // 设置图片路径
            asReportImageMapper.insert(image);
        }
    }

    @Override
    public Map<String, Object> queryReportManagement(Integer ment) {
        Map<String, Object> map = new HashMap<>();
        List<AsReportManagement> asReportManagements = asReportManagementMapper.selectList(null);
        map.put("asReportManagements",asReportManagements);
        if(ment!=null){
            LambdaQueryWrapper<AsReportManagement> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AsReportManagement::getMent,ment);
            List<AsReportManagement> selectList = asReportManagementMapper.selectList(wrapper);
            map.put("selectList",selectList);
        }
        return map;
    }

    @Override
    public void updateReportManagement(AsReportManagement asReportManagement) {
        if (Objects.equals(asReportManagement.getMent(), State.one) || Objects.equals(asReportManagement.getMent(), State.two)){
            AsReportManagement management = new AsReportManagement();
            management.setMent(State.five);
            asReportManagementMapper.updateById(management);
        }
    }

    @Override
    public List<Object> selectReportManagement(AsReportManagement asReportManagement) {
        List<Object> list = new ArrayList<>();
        List<AsReportManagement> asReportManagements = asReportManagementMapper.selectList(null);
        for (AsReportManagement reportManagement : asReportManagements) {
            LambdaQueryWrapper<AsReportImage> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AsReportImage::getReportId, reportManagement.getId());
            List<AsReportImage> asReportImages = asReportImageMapper.selectList(wrapper);
            list.add(asReportImages);
        }
        return list;
    }

    @Override
    public void deleteReportManagement(Integer id) {
        asReportManagementMapper.deleteById(id);
    }


}
